// pages/about_cate/tab_cate/tab_cate.js
Page({

  data: {
    top_tab: [
      '生活电器',
      '生活电器',
      '生活电器',
      '数码',
      '生活电器',
      '生活电器',
      '生活电器',
      '生活电器'
    ],

    cate: [{
        title: '高性价比的智能小家电',
        goods_list: [{
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          }
        ]
      },
      {
        title: '高性价比的智能小家电',
        goods_list: [{
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          }
        ]
      },
      {
        title: '高性价比的智能小家电',
        goods_list: [{
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          }
        ]
      },
      {
        title: '高性价比的智能小家电',
        goods_list: [{
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          }
        ]
      },
      {
        title: '高性价比的智能小家电',
        goods_list: [{
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          }
        ]
      },
      {
        title: '高性价比的智能小家电',
        goods_list: [{
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          }
        ]
      },
      {
        title: '高性价比的智能小家电',
        goods_list: [{
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          }
        ]
      },
      {
        title: '高性价比的智能小家电',
        goods_list: [{
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          },
          {
            img: '/images/goods.jpg',
            is_new_goods: 1,
            name: '驱蚊魔方',
            price: '120.00'
          }
        ]
      }
    ],

    // 商品元素高度
    item_H: null,

    swiper_H: null,

    // scroll位置
    scrollLeft: 0,

    // 当前显示
    current_index: 0
  },

  // 切换tab栏
  switch_top_tab(e) {
    this.setData({
      current_index: e.currentTarget.dataset.index
    })

    this.checkCor();
    this.set_h();    
  },

  // 轮播图当前显示改变
  switchTab(e) {
    this.setData({
      current_index: e.detail.current
    })

    this.checkCor();
    this.set_h()    
  },

  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor() {
    if (this.data.current_index > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },

  onLoad() {
    this.set_h()
  },

  // 获取高度
  get_H(callback) {
    var that = this;
    var query = wx.createSelectorQuery()
    query.select('.item_').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec(function(res) {
      var h = res[0].height;
      callback(h)
    })
  },

  set_h() {
    var that = this;
    this.get_H(function(h) {
      var data = that.data.cate;
      var length = data[that.data.current_index].goods_list.length;
      console.log('长度:' + length)
      console.log('高度:' + h)
      console.log(Math.ceil(length / 2))

      that.setData({
        swiper_H: Math.ceil(length) * h + 500
      })
    })
  }





})